import type { App } from 'vue'
import { createI18n } from 'vue-i18n'
import enUS from './en-US'
import zhCN from './zh-CN'
import zhTW from './zh-TW'
import hiIN from './hi-IN'
import itIT from './it-it'
import ukUA from './uk-ua'
import koKR from './ko-kr'
import ptPT from './pt-pt'
import filPH from './fil-ph'
import thTH from './th-th'
import jaJP from './ja-jp'
import idID from './id-id'
import frFR from './fr-fr'
import arSA from './ar-sa'
import viVN from './vi-vn'
import ruRU from './ru-ru'
import trTR from './tr-tr'
import esES from './es-es'
const messages = {
  'en-us': enUS,
  'en-gb': enUS,
  'zh-cn': zhCN,
  'zh-tw': zhTW,
  'hi-in': hiIN,
  'it-it': itIT,
  'uk-ua': ukUA,
  'ko-kr': koKR,
  'pt-pt': ptPT,
  'fil-ph': filPH,
  'th-th': thTH,
  'ja-jp': jaJP,
  'id-id': idID,
  'fr-fr': frFR,
  'ar-sa': arSA,
  'vi-vn': viVN,
  'ru-ru': ruRU,
  'tr-tr': trTR,
  'es-es': esES
}

import { getLocalSetting, Language } from '@/store/modules/app/helper'

let state = getLocalSetting();
let defaultLocale = '';
if(state) {
  defaultLocale = state.language;
}else {
  let lang = navigator.language.toLowerCase();
  if(lang.length <=2) {
    defaultLocale = Object.keys(messages).find(k => k.startsWith(lang)) as string;
  }else {
    defaultLocale = lang;
  }
}

const i18n = createI18n({
  locale: defaultLocale,
  fallbackLocale: 'en-us',
  allowComposition: true,
  messages
})

export const t = i18n.global.t

export function setLocale(locale: Language) {
  console.log(locale)
  i18n.global.locale = locale
}

export function setupI18n(app: App) {
  app.use(i18n)
}

export default i18n
